from sanic import Sanic
import aiomysql
import aioredis
from sanic import response
import asyncio
from product_api import SpecificMethoD

SpecificMethod = SpecificMethoD()
import json

from . import bp_product



@bp_product.route("/product_type")
async def product_type_info(request):

    if not request.method == 'GET':
    
        return response.json({
                'status': -1,
                'errorCode': 1,
                'errorMsg':f"请求方式error,request.method:{request.method}",
                'resultBody':None
            })

    product_all_type = await SpecificMethod.get_str('product_all_type')

    if product_all_type != None:
        #'从redis中取'
        product_all_type = json.loads(product_all_type)

    elif product_all_type == None:
        '从数据库中取'
        product_all_type = await SpecificMethod.select_info(sql="select * from product_type")

        if product_all_type == None:
            
            return response.json({
                                    'status': 1,
                                    'errorCode': None,
                                    'errorMsg':None,
                                    'resultBody':product_all_type
                                })

        # print(product_all_type)
        product_all_type = str(product_all_type)
        product_all_type_json = json.dumps(product_all_type)
        await SpecificMethod.set_str('product_all_type', product_all_type_json)

    return response.json({
                            'status': 1,
                            'errorCode': None,
                            'errorMsg':None,
                            'resultBody':product_all_type
                        })

@bp_product.route("/product_type_id",methods=['POST'])
async def product_type_info(request):

    if not request.method == 'POST':
    
        return response.json({
                'status': -1,
                'errorCode': 1,
                'errorMsg':f"请求方式error,request.method:{request.method}",
                'resultBody':None
            })

    product_type_id = request.form.get('product_type_id')
    
    product_type = await SpecificMethod.select_info(sql="select * from product_type where id=%s;",args=product_type_id)

    if product_type == None:
        return response.json({
                                'status': 1,
                                'errorCode': None,
                                'errorMsg':None,
                                'resultBody':product_type
                            })
        
    return response.json({
                            'status': 1,
                            'errorCode': None,
                            'errorMsg':None,
                            'resultBody':product_type
                        })


@bp_product.route("/add_product_type",methods=['POST',])

async def insert_producttype_work(request):

    if not request.method == 'POST':

        return response.json({
                'status': -1,
                'errorCode': 1,
                'errorMsg':f"请求方式error,request.method:{request.method}",
                'resultBody':None
            })

    info = request.form

    product_type = info.get("product_type")

    if not product_type:

        return response.json({
                'status': -1,
                'errorCode': 16,
                'errorMsg':f"缺少参数product_type{product_type}",
                'resultBody':None
            })

    all_product_type = await SpecificMethod.select_info(sql="select product_type from product_type")

    print(all_product_type)

    if {'product_type': product_type} in all_product_type:
        
        return response.json({
                'status': -1,
                'errorCode': 17,
                'errorMsg':f"添加商品分类失败请检查product_type{product_type}是否已创建",
                'resultBody':None
            })
        

    work = await SpecificMethod.insert_info(sql="insert into product_type(product_type)values(%s);",args=product_type)

    if not work:

        return response.json({
                'status': -1,
                'errorCode': 18,
                'errorMsg':f"添加商品分类失败请检查product_type{product_type}格式",
                'resultBody':None
            })

    await SpecificMethod.delete_str('product_all_type')

    return response.json({
                'status': 1,
                'errorCode': None,
                'errorMsg':None,
                'resultBody':'添加商品分类成功'
            })
    
@bp_product.route("/delete_product_type",methods=['POST',])

async def delete_producttype_work(request):

    if not request.method == 'POST':

        return response.json({
                'status': -1,
                'errorCode': 1,
                'errorMsg':f"请求方式error,request.method:{request.method}",
                'resultBody':None
            })

    info = request.form

    product_type = info.get("product_type")

    if not product_type:

        return response.json({
                'status': -1,
                'errorCode': 19,
                'errorMsg':f"缺少参数product_type{product_type}",
                'resultBody':None
            })

    work = await SpecificMethod.insert_info(sql="DELETE FROM product_type WHERE product_type=%s",args=product_type)

    if not work:

        return response.json({
                'status': -1,
                'errorCode': 20,
                'errorMsg':f"删除商品分类失败请检查product_type{product_type}格式",
                'resultBody':None
            })

    await SpecificMethod.delete_str('product_all_type')

    return response.json({
                'status': 1,
                'errorCode': None,
                'errorMsg':None,
                'resultBody':'删除商品分类成功'
            })

@bp_product.route("/update_product_type",methods=['PUT'])

async def update_producttype_work(request):

    if not request.method == 'PUT':

        return response.json({
                'status': -1,
                'errorCode': 1,
                'errorMsg':f"请求方式error,request.method:{request.method}",
                'resultBody':None
            })

    info = request.form

    old_product_type = info.get("old_product_type")

    new_product_type = info.get("new_product_type")

    if not all([old_product_type,new_product_type]):

        return response.json({
                'status': -1,
                'errorCode': 21,
                'errorMsg':f"缺少参数old_product_type:{old_product_type},new_product_type:{new_product_type}",
                'resultBody':None
            })

    work = await SpecificMethod.insert_info(sql="UPDATE product_type SET product_type=%s WHERE product_type=%s",args=(new_product_type,old_product_type,))

    if not work:

        return response.json({
                'status': -1,
                'errorCode': 22,
                'errorMsg':f"更新商品分类失败请检查old_product_type:{old_product_type},new_product_type:{new_product_type}格式",
                'resultBody':None
            })

    await SpecificMethod.delete_str('product_all_type')

    return response.json({
                'status': 1,
                'errorCode': None,
                'errorMsg':None,
                'resultBody':'更新商品分类成功'
            })